<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >

            <el-form ref="formRef" :model="formData" label-width="100px" :rules="formRules">
                <el-form-item label="队伍1分数" prop="playOneScore">
                    <el-input v-model="formData.playOneScore" placeholder="请输入队伍1分数">
                      <template #suffix>
                        分
                      </template>
                    </el-input>
                </el-form-item>
                <el-form-item label="队伍2分数" prop="playTwoScore">
                    <el-input v-model="formData.playTwoScore" placeholder="请输入队伍2分数">
                      <template #suffix>
                        分
                      </template>
                    </el-input>
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {
  competitionEdit,
  competitionAdd,
  competitionDetail,
  competionScheduleDetail, competitionScheduleEdit
} from '@/api/competition/competition'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '赛程调整' : '新增赛程'
})

const formData = reactive({
    name: '',
    orgParty: '',
    id: '',
    competitionId: '',
    playType: '',
    orderNum: '',
    playOneNo: '',
    playTwoNo: '',
    playOneScore: '',
    playTwoScore: '',
    startTime: '',
    result: '',
    status: '',
    schedule: '',
    addressName: '',
    address: '',
    playOneName: '',
    playTwoName: '',
})

const formRules = {
  playOneScore: [
      {
          required: true,
          message: '请输入队伍1分数',
          trigger: ['blur']
      }
  ],
  playTwoScore: [
      {
          required: true,
          message: '请输入队伍2分数',
          trigger: ['blur']
      }
  ]
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await competitionScheduleEdit(data) : await competitionScheduleEdit(data)
    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await competionScheduleDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
